package com.bos.prop.controller.json;

import com.bos.dao.KabupatenDAO;
import com.bos.domain.Kabupaten;
import com.bos.editor.KabupatenEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/18/11
 * Time: 3:06 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class KabupatenJsonController {

    private KabupatenDAO kabupatenDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/kabupaten.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Kabupaten> index() {
        try {
            return kabupatenDAO.getAll();
        } catch (DataAccessException e) {
            return new ArrayList<Kabupaten>();
        }
    }

    @RequestMapping(value = "/kabupaten/{id}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Kabupaten show(@PathVariable("id") Kabupaten kabupaten) {
        return kabupaten;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
    }

}
